'use strict'

async function query(sql) {
    //连接mysql2
    const mysql = require('mysql2/promise');
    // 创建连接
    const connection = await mysql.createConnection({host:'49.233.107.127', user: 'root', password:'nijuxiaozu',database: 'test'});
    // 执行sql语句
    const [rows, fields] = await connection.execute(sql);
    return rows;
  }
const titbit=require('titbit');

const {httpcli} = require('gohttp');
let cert_path='/usr/local/share';
const app=new titbit({
    //开启调试模式，会输出错误信息
    debug:true,
    globalLog:true,
    cert:`${cert_path}/wx.qasdwer.top.pem`,
    key:`${cert_path}/wx.qasdwer.top.key`
})

app.get('/login',async c=>{
    let url='https://api.weixin.qq.com/sns/jscode2session?appid=wxbf2045ee83472f91&secret=78334351a17ccdab23143a9d7a0dea6f&js_code=' + c.query.code + '&grant_type=authorization_code'
    let ret=await httpcli.get(url)
                        .then(res=>{
                            return res.json()
                        })
    // console.log(ret.openid)
    c.send(ret.openid)
})
app.get('/home', async c=>{  
    //返回首页文化数据
    let sql=`select cultureID,cultureTitle,cultureImg,cultureCon from culture where cityID in(select cityID from city where cityName="${c.query.city}")`;
    let r=await query(sql)
    c.send(r)
})
app.get('/home/lunbo',async c=>{
    //返回首页轮播图
    let sql=`select cityimage1,cityimage2,cityimage3 from image where cityID in(select cityID from city where cityName="${c.query.city}")`;
    let r=await query(sql)
    c.send(r)
})
app.get('/rim', async c=>{
    //根据info的值来返回不同的数据(推荐，景点，美食，酒店)
    if (c.query.info=='tuijian'){
        let sql=`select recomID,recomName,recomImg,recomItro,type from recommend where cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.info=='jingdian'){
        let sql=`select placeID,placeName,placeImg,placeItro from place where cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.info=='meishi'){
        let sql=`select foodID,foodName,foodImg,foodAddr,foodTalk from food where cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }
    else if(c.query.info=='jiudian'){
        let sql=`select pubID,pubName,pubImg,pubAddr,pubNo,pubTalk from pub where cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r);
    }
})
app.get('/historyDetail',async c=>{
    //返回历史详情页内容
    let sql=`select cultureTitle,cultureImg,cultureCon from culture where cultureID="${c.query.index}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
    let r=await query(sql);
    c.send(r)
})
app.get('/recommend_detail', async c=>{
    //返回周边推荐详情页内容
    if(c.query.type==1){
        let sql=`select cityID,placeID,placeName,placeImg,placeTime,placeAddr,longitude,latitude from place where placeName="${c.query.name}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.type==2){
        let sql=`select cityID,foodID,foodName,foodImg,foodAddr,foodType,foodTalk,foodTime,ufoodTalk,foodRecom from food where foodName="${c.query.name}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.type==3){
        let sql=`select cityID,pubID,pubName,pubImg,pubAddr,pubTime,pubinimage,pubType,upubTalk from pub where pubName="${c.query.name}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }
})
app.get('/rim_detail',async c=>{
    //返回周边除推荐详情页内容
    if(c.query.info=='jingdian'){
        //返回景点名称，图片，开放时间，位置，经纬度
        let sql=`select cityID,placeID,placeName,placeImg,placeTime,placeAddr,longitude,latitude from place where placeID="${c.query.index}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.info=='meishi'){
        //返回食物名，图片，位置，食物类型（火锅，烧烤），食物评价，开放时间，顾客评价，推荐菜
        let sql=`select cityID,foodID,foodName,foodImg,foodAddr,foodType,foodTalk,foodTime,ufoodTalk,foodRecom from food where foodID="${c.query.index}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }
    else if(c.query.info=='jiudian'){
        //返回酒店名，图片，位置，开放时间，房间图片，酒店类型（舒适，经济），顾客评价
        let sql=`select cityID,pubID,pubName,pubImg,pubAddr,pubTime,pubinimage,pubType,upubTalk from pub where pubID="${c.query.index}" and cityID in(select cityID from city where cityName="${c.query.city}")`;
        let r=await query(sql);
        c.send(r)
    }
})
//进入首页的搜索结果页
app.get('/search_home',async c=>{
    let sql=`select * from culture where cultureTitle REGEXP '${c.query.id}' or cultureCon REGEXP '${c.query.id}'`
    let r=await query(sql);
    c.send(r)
})
//进入首页搜索结果详情页
app.get('/search_home_detail',async c=>{
    let sql=`select cultureTitle,cultureImg,cultureCon from culture where cultureID="${c.query.culture}" and cityID='${c.query.city}'`;
    let r=await query(sql);
    c.send(r)
})
//进入周边的搜索结果页
app.get('/search_rim',async c=>{
    let r=[];
    let sql1=`select * from food where foodName REGEXP '${c.query.id}'`
    r[0]=await query(sql1);
    let sql2=`select * from place where placeName REGEXP '${c.query.id}' or placeAddr REGEXP '${c.query.id}'`
    r[1]=await query(sql2);
    let sql3=`select * from pub where pubName REGEXP '${c.query.id}' or pubAddr REGEXP '${c.query.id}'`
    r[2]=await query(sql3);
    let newarr=r[0].concat(r[1]).concat(r[2]);
    c.send(newarr)
})
app.get('/weather',async c=>{
    //获取天气图片
    let sql=`select image from weather where textDay='${c.query.textDay}';`;
    let r=await query(sql);
    c.send(r);
}) 
//进入周边的搜索结果详情页
app.get('/search_rim_detail',async c=>{
    //返回周边除推荐详情页内容
    if(c.query.type=='jingdian'){
        //返回景点名称，图片，开放时间，位置，经纬度
        let sql=`select placeName,placeImg,placeTime,placeAddr,longitude,latitude from place where placeID="${c.query.id}" and cityID='${c.query.city}'`;
        let r=await query(sql);
        c.send(r)
    }else if(c.query.type=='meishi'){
        //返回食物名，图片，位置，食物类型（火锅，烧烤），食物评价，开放时间，顾客评价，推荐菜
        let sql=`select foodName,foodImg,foodAddr,foodType,foodTalk,foodTime,ufoodTalk,foodRecom from food where foodID="${c.query.id}" and cityID='${c.query.city}'`;
        let r=await query(sql);
        c.send(r)
    }
    else if(c.query.type=='jiudian'){
        //返回酒店名，图片，位置，开放时间，房间图片，酒店类型（舒适，经济），顾客评价
        let sql=`select pubName,pubImg,pubAddr,pubTime,pubinimage,pubType,upubTalk from pub where pubID="${c.query.id}" and cityID='${c.query.city}'`;
        let r=await query(sql);
        c.send(r)
    }
})
if (process.argv.indexOf('--local') > 0) {
    app.config.https = false
    app.config.cert = ''
    app.config.key = ''
  }
app.run(2001)